var paraurl = "/";
/*
 * 1,canvas绘制进度条（无依赖）
 * <canvas id="min" width="120" height="120"></canvas>
 * CircleProcess(document.getElementById("min"), {
        "size": "complete", //complete圆,half半圆,incomplete多半圆 or todo-percent半圆
        "percent": 90,//进度
        "process": 80,
        "radius":50,//半径
        "backgroundCircle": {
            "color": "#ccc"//背景
            },
        "percentCircle": {
            "show": true,//是否绘制进度
            "color": "red",
            "speed": "gradient", //gradient normal fast(进度条渐变方式)
            "gradientColorShow": true
            },
        "startSmallCircle": {//起始位置圆
            "show": false,
            "radius": 5,
            },
        "endSmallCircle": {//终止位置圆
            "show": false,
            "radius": 5
            },
        "processText": {//文字
            "show": true,
            "color": "blue",
            "follow": true
            }
        });
 */
if (!window.zm) {
	zm = {};
};
var CircleProcess = (function (c) {
	var a = "0.0.1";
	var b = function (d, e) {
		return new b.fn.init(d, e)
	};
	b.fn = b.prototype = {
		circleProcess: a,
		context: null,
		option: {},
		interVal: 0,
		constructor: b,
		init: function (d, e) {
			if (typeof d === "undefined") {
				throw new Error("Unable to get canvas object")
			}
			this.context = d.getContext("2d");
			this.option = this._option();
			this._getCanvasOption(d);
			this._getOption();
			if (typeof e === "object") {
				this._setOption(e);
				this._getOption()
			}
			this.interVal = setInterval(function () {
				this.run()
			}.bind(this), 20)
		},
		run: function () {
			var d = this.option;
			var e = d.process;
			if (d.percent > 100 || d.percent < 0) {
				clearInterval(this.interVal);
				alert("Percentage out of range,The correct range of 1 - 100");
				throw new Error("Percentage out of range,The correct range of 1 - 100")
			}
			if (e >= d.percent) {
				e = d.percent;
				clearInterval(this.interVal)
			}
			this._clear();
			if (d.backgroundCircle.show) {
				this._backgroundCircle()
			}
			if (d.percentCircle.show) {
				this._percentCircle(e)
			}
			if (d.startSmallCircle.show) {
				this._smallCircle(d.startSmallCircle.radius, "#06a8f3", 0)
			}
			if (d.endSmallCircle.show) {
				this._smallCircle(d.endSmallCircle.radius, "#00f8bb", e)
			}
			if (d.processText.show) {
				this._showProcess(e)
			}
			this._speed(d.percentCircle.speed)
		},
		_clear: function () {
			var d = this.option;
			this.context.clearRect(0, 0, d.backgroundCircle.roundX * 2, d.backgroundCircle.roundY * 2)
		},
		_backgroundCircle: function () {
			var d = this.option;
			this.context.lineWidth = d.lineWidth;
			this.context.beginPath();
			this.context.arc(d.backgroundCircle.roundX, d.backgroundCircle.roundY, d.radius, d.backgroundCircle.startAngle, d.backgroundCircle.endAngle);
			this.context.strokeStyle = d.backgroundCircle.color;
			this.context.stroke();
			this.context.closePath()
		},
		_percentCircle: function (e) {
			var d = this.option;
			this.context.beginPath();
			this.context.lineWidth = d.lineWidth;
			this.context.arc(d.backgroundCircle.roundX, d.backgroundCircle.roundY, d.radius, d.backgroundCircle.startAngle, d.backgroundCircle.startAngle + e / 100 * d.cMultiple, false);
			if (d.percentCircle.gradientColorShow) {
				this.context.strokeStyle = this._linearGradient()
			} else {
				this.context.strokeStyle = d.percentCircle.color
			}
			this.context.lineCap = "round";
			this.context.stroke();
			this.context.closePath()
		},
		_linearGradient: function () {
			var f = this.option;
			var i = f.backgroundCircle.roundX - f.radius - this.option.lineWidth;
			var d = f.backgroundCircle.roundX + f.radius + this.option.lineWidth;
			var g = f.backgroundCircle.roundY;
			var h = f.backgroundCircle.roundY;
			var e = this.context.createLinearGradient(i, g, d, h);
			e.addColorStop(0, "rgba(255,0,0,0.1)");
			e.addColorStop(0.2, "rgba(255,0,0,0.3)");
			e.addColorStop(0.4, "rgba(255,0,0,0.5)");
			e.addColorStop(0.6, "rgba(255,0,0,0.7)");
			e.addColorStop(0.8, "rgba(255,0,0,0.9)");
			e.addColorStop(1, "rgba(255,0,0,1)");
			return e
		},
		_smallCircle: function (e, f, h) {
			var g = this.option;
			var d = Math.cos(2 * Math.PI / 360 * (g.angle + h * g.sMultiple / 100)) * g.radius + g.backgroundCircle.roundX;
			var i = Math.sin(2 * Math.PI / 360 * (g.angle + h * g.sMultiple / 100)) * g.radius + g.backgroundCircle.roundY;
			this.context.beginPath();
			this.context.arc(d, i, e, 0, Math.PI * 2);
			this.context.lineWidth = 1;
			this.context.fillStyle = f;
			this.context.fill()
		},
		_showProcess: function (g) {
			var d = 0;
			var f = this.option;
			var e = f.percent.toString();
			if (/\./.test(e) && e.split(".")[1].length > 0) {
				d = e.split(".")[1].length < 2 ? 1 : 2
			}
			this.context.font = f.processText.fontSize + "px April";
			this.context.textAlign = f.processText.textAlign;
			this.context.textBaseline = f.processText.textBaseline;
			this.context.fillStyle = f.processText.color;
			this.context.fillText(parseFloat(g).toFixed(d) + "%", f.backgroundCircle.roundX, f.backgroundCircle.roundY)
		},
		_showProcessFollow: function () { },
		_speed: function (e) {
			var d = this.option;
			if (e === "gradient") {
				if (d.process / d.percent > 0.9) {
					d.process += 0.3
				} else {
					if (d.process / d.percent > 0.8) {
						this.option.process += 0.55
					} else {
						if (d.process / d.percent > 0.7) {
							d.process += 0.75
						} else {
							d.process += 1
						}
					}
				}
			} else {
				if (e === "normal") {
					d.process += 1
				} else {
					if (e === "fast") {
						d.process += 2.5
					}
				}
			}
		},
		_getCanvasOption: function (d) {
			this._extend(this.option, {
				backgroundCircle: {
					roundX: d.width / 2,
					roundY: d.height / 2,
					startAngle: Math.PI / 180 * 180,
					endAngle: Math.PI / 180 * 180 * 2
				}
			})
		},
		_extend: function (e, f) {
			for (var d in f) {
				if (typeof f[d] === "object") {
					this._extend(e[d], f[d])
				} else {
					if (f.hasOwnProperty(d) && (!e.hasOwnProperty(d))) {
						e[d] = f[d]
					} else {
						if (f.hasOwnProperty(d) && e.hasOwnProperty(d)) {
							e[d] = f[d]
						}
					}
				}
			}
		},
		_getOption: function () {
			var d = this.option;
			d.angle = 180;
			d.cMultiple = d.backgroundCircle.startAngle;
			d.sMultiple = 180;
			if (d.size === "complete") {
				d.angle = 120;
				d.cMultiple = d.backgroundCircle.startAngle * 2;
				d.sMultiple = 360;
				this._extend(this.option, {
					backgroundCircle: {
						startAngle: Math.PI / 180 * 120,
						endAngle: Math.PI / 180 * 120 * 4
					}
				})
			} else {
				if (d.size === "incomplete") {
					d.angle = 120;
					d.cMultiple = Math.PI * 5 / 3;
					d.sMultiple = 300;
					this._extend(this.option, {
						backgroundCircle: {
							startAngle: Math.PI / 180 * 120,
							endAngle: Math.PI / 180 * 420
						}
					})
				}
			}
		},
		_setOption: function (d) {
			this._extend(this.option, d)
		},
		_option: function () {
			return {
				size: "half",
				lineWidth: 5,
				radius: 100,
				percent: 80,
				process: 0,
				backgroundCircle: {
					show: true,
					color: "#eee",
					roundX: 0,
					roundY: 0,
					startAngle: 0,
					endAngle: 0
				},
				percentCircle: {
					show: true,
					color: "#f00",
					speed: "gradient",
					gradientColorShow: false,
					gradientColor: "rgba(255.0.0,0.1)"
				},
				startSmallCircle: {
					show: false,
					color: "#06a8f3",
					radius: 5
				},
				endSmallCircle: {
					show: false,
					color: "#00f8bb",
					radius: 5
				},
				gradient: {
					color: "rgba(255,0,0,0.1)",
					size: 4
				},
				processText: {
					show: true,
					fontSize: 20,
					color: "#ccc",
					follow: false,
					textAlign: "center",
					textBaseline: "alphabetic"
				}
			}
		}
	};
	b.fn.init.prototype = b.fn;
	return b
})();

/**
 * 将时间转换成时间戳zm.gTimes()
 * @param DateTime 为时间格式下的时间 2018/06/14 13:00:00或2018-06-14 13:00:00
 * @returns {number}
 * @constructor
 */
zm.gTimes = function (DateTime) {
	var oDate = new Date(Date.parse(DateTime.replace(/-/g, "/")));
	var Unix = oDate.getTime();
	return Unix;
}
/**
 * 2,时间转换zm.gTime(UnixTime,ShowTime)；
 * UnixTime必须为number如：zm.gTime(Number(time),3)
 * ShowTime有几个值，每个只对应不同的时间格式 值区间为0-13 为0时不用传值。为默认显示格式
 * ShowTime =1  2018-06-15 14:43:20
 * ShowTime =2  2018/06/15 14:43:20
 * ShowTime =3  2018年06月15日 14:43:20
 * ShowTime =4  2018年06月15日
 * ShowTime =5  2018/06/15
 * ShowTime =6  2018-06-15
 * ShowTime =7  14:43:20
 * ShowTime =8  14时43分20秒
 * ShowTime =9  星期1
 * ShowTime =10  二零一八年六月二三日 星期六
 * ShowTime =11  2018-06-23 11:20:14 星期6
 * ShowTime =12  2018/06/23 11:20:14 星期6
 * ShowTime =13  2018年06月23日 11时20分14秒 星期6
 */

zm.gTime = function (UnixTime, ShowTime) {
	var ToUnix = new Date(Number(UnixTime));
	var Years = ToUnix.getFullYear(); //获取年 例子：2018
	var Month = ToUnix.getMonth() + 1; //获取月(0-11,0代表1月)
	var Day = ToUnix.getDate(); //获取日（0-31）
	var Week = ToUnix.getDay(); //获取星期（0-6；0代表星期天）
	var Hours = ToUnix.getHours(); //获取小时（0-23）
	var Minutes = ToUnix.getMinutes(); //获取分钟（0-59）
	var Seconds = ToUnix.getSeconds(); //获取秒
	var DaTime;
	if (ShowTime == 2) {
		DaTime = Years + "/" + DeCa(Month) + "/" + DeCa(Day) + " " + DeCa(Hours) + ":" + DeCa(Minutes) + ":" + DeCa(Seconds);
	} else if (ShowTime == 3) {
		DaTime = Years + "年" + DeCa(Month) + "月" + DeCa(Day) + "日 " + DeCa(Hours) + ":" + DeCa(Minutes) + ":" + DeCa(Seconds);
	} else if (ShowTime == 4) {
		DaTime = Years + "年" + DeCa(Month) + "月" + DeCa(Day) + "日";
	} else if (ShowTime == 5) {
		DaTime = Years + "/" + DeCa(Month) + "/" + DeCa(Day);
	} else if (ShowTime == 6) {
		DaTime = Years + "-" + DeCa(Month) + "-" + DeCa(Day);
	} else if (ShowTime == 7) {
		DaTime = DeCa(Hours) + ":" + DeCa(Minutes) + ":" + DeCa(Seconds);
	} else if (ShowTime == 8) {
		DaTime = DeCa(Hours) + "时" + DeCa(Minutes) + "分" + DeCa(Seconds) + "秒";
	} else if (ShowTime == 9) {
		DaTime = "星期" + Week;
	} else if (ShowTime == 10) {
		DaTime = NumBerToHanZi(Years) + "年" + NumBerToHanZi(Month) + "月" + NumBerToHanZi(Day) + "日 星期" + NumBerToHanZi(Week);
	} else if (ShowTime == 11) {
		DaTime = Years + "-" + DeCa(Month) + "-" + DeCa(Day) + " " + DeCa(Hours) + ":" + DeCa(Minutes) + ":" + DeCa(Seconds) + " 星期" + Week;
	} else if (ShowTime == 12) {
		DaTime = Years + "/" + DeCa(Month) + "/" + DeCa(Day) + " " + DeCa(Hours) + ":" + DeCa(Minutes) + ":" + DeCa(Seconds) + " 星期" + Week;
	} else if (ShowTime == 13) {
		DaTime = Years + "年" + DeCa(Month) + "月" + DeCa(Day) + "日 " + DeCa(Hours) + "时" + DeCa(Minutes) + "分" + DeCa(Seconds) + "秒 星期" + Week;
	} else {
		DaTime = Years + "-" + DeCa(Month) + "-" + DeCa(Day) + " " + DeCa(Hours) + ":" + DeCa(Minutes) + ":" + DeCa(Seconds);
	}
	return DaTime;
}

function DeCa(Natural) {
	var NaturalNum;
	if (Natural < 10) {
		NaturalNum = "0" + Natural;
	} else {
		NaturalNum = Natural;
	}
	return NaturalNum;
}
//将阿拉伯数字转换成汉字
zm.gHanzi = function (Numbers) {
	var strIns, chnStr = '';
	var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
	while (Numbers > 0) {
		var v = Numbers % 10;
		strIns = chnNumChar[v];
		chnStr = chnNumChar[v] + chnStr;
		Numbers = Math.floor(Numbers / 10);
	}
	return chnStr;
}
// 计算时间差
zm.gTimec = function (EndTime) {
	//如果为时间戳
	var EndTimes = new Date(EndTime).getTime(); //结束时间

	var NowTime = new Date().getTime(); //当前时间

	var DeltaT = EndTimes - NowTime;
	//计算出相差天数
	var days = Math.floor(DeltaT / (24 * 3600 * 1000));

	//计算出小时数

	var leave1 = DeltaT % (24 * 3600 * 1000);
	var H = Math.floor(leave1 / (3600 * 1000));
	//计算相差分钟数
	var leave2 = leave1 % (3600 * 1000);
	var M = Math.floor(leave2 / (60 * 1000));
	//计算相差秒数
	var leave3 = leave2 % (60 * 1000);
	var S = Math.round(leave3 / 1000);
	var reminder;
	if (DeltaT > 0) {
		if (days != "") {
			reminder = days + "天 " + H + "小时 " + M + " 分钟" + S + " 秒";
		} else if (days == "" || H != "") {
			reminder = H + "小时 " + M + " 分钟" + S + " 秒";
		}
	} else {
		reminder = "请注意！时间到了！";
	}
	return reminder;
}
//取页面url参数值var gps = zm.gParaurl()  var id = gps.id
zm.gParaurl = function () {
	var qs = {};
	var url = decodeURIComponent(window.location.href);
	if (zm.isredirect) {
		if (url.indexOf('htm') > -1) {
			url = url.substring(url.indexOf('/', 7) + 1);
			var paras = ["pageid", "id"];
			var prm = url.split('/');
			if (url.indexOf("_") > -1) {
				prm = url.split('_')
			}
			for (var p in prm) {
				var index = prm[p].split('.');
				if (/^\d+$/.test(index[0])) {
					qs[paras[p]] = index[0]
				}
			}
		} else {
			url = url.substring(url.indexOf('/', 7) + 1);
			var prm = url.split('/');
			if (/^\d+$/.test(prm[0])) {
				qs.pageid = prm[0]
			}
		}
	}
	if (url.indexOf('?') > -1) {
		url = url.substring(url.indexOf('?') + 1);
		var prm = url.split('&');
		for (var p in prm) {
			if (prm[p]) {
				var sp = prm[p].split('=');
				if (sp.length > 1) {
					var spkey = sp[0];
					var spvalue = sp[1];
					if (spvalue.indexOf('#') > -1) {
						spvalue = spvalue.substring(0, spvalue.indexOf('#'))
					}
					qs[spkey] = spvalue
				}
			}
		}
	}
	return qs
};
/**
* 弹出警示框
* @param mes       提示文字String 【必填】
* @param callback  回调函数Function 【可选】
*/
zm.alert = function (mes, callback) {
	var $body = $("body");
	var ID = 'ZM_ALERT';
	$('#' + ID).remove();
	var $dom = $('' +
		'<div id="' + ID + '">' +
		'   <div class="mask-black-dialog">' +
		'       <div class="m-confirm m-alert">' +
		'           <div class="confirm-bd">' + (mes) + '</div>' +
		'           <div class="confirm-ft">' +
		'               <a href="javascript:;" class="confirm-btn primary">确定</a>' +
		'           </div>' +
		'       </div>' +
		'   </div>' +
		'</div>');
	$body.append($dom);
	$dom.find('a').on('click', function () {
		$dom.remove();
		typeof callback === 'function' && callback();
	});
};
/**
 * @param mes       提示文字String 【必填】
 * @param type      类型String success or error 【必填】
 * @param timeout   多久后消失Number 毫秒 【默认：2000ms】【可选】
 * @param callback  回调函数Function 【可选】
 */
zm.toast = function () {
	var timer = null;
	var $body = $("body");
	return function (mes, type, timeout, callback) {

		clearTimeout(timer);

		var ID = 'YDUI_TOAST';

		$('#' + ID).remove();

		var args = arguments.length;
		if (args < 2) {
			console.error('From YDUI\'s toast: Please set two or more parameters!!!');
			return;
		}

		var iconHtml = '';
		if (type == 'success' || type == 'error') {
			iconHtml = '<div class="' + (type == 'error' ? 'toast-error-ico' : 'toast-success-ico') + '"></div>';
		}

		var $dom = $('' +
			'<div class="mask-white-dialog" id="' + ID + '">' +
			'    <div class="m-toast ' + (iconHtml == '' ? 'none-icon' : '') + '">' + iconHtml +
			'        <p class="toast-content">' + (mes || '') + '</p>' +
			'    </div>' +
			'</div>');

		$body.append($dom);

		if (typeof timeout === 'function' && arguments.length >= 3) {
			callback = timeout;
			timeout = 2000;
		}

		timer = setTimeout(function () {
			clearTimeout(timer);
			$dom.remove();
			typeof callback === 'function' && callback();
		}, (~~timeout || 2000) + 100);//100为动画时间
	};
}();
